package com.eliota.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eliota.common.R;
import com.eliota.controller.dto.DeviceDto;
import com.eliota.entity.Device;
import com.eliota.mapper.DeviceMapper;
import com.eliota.mapper.DeviceStatusCountMapper;
import com.eliota.service.DeviceService;
import com.eliota.service.TypeService;
import com.eliota.service.UserService;
import com.eliota.service.impl.DeviceStatusCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MG
 * @since 2023-04-01
 */
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    @Resource
    private UserService userService;

    @Resource
    private TypeService typeService;

    @Resource
    private DeviceMapper deviceMapper;


    @Resource
    private DeviceStatusCountService deviceStatusCountService;

    /**
     * 操作device借出设备，对project_device表操作
     * @param device
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Device device){
        return R.success(deviceService.saveOrUpdate(device));
    }

    /*userNam*/
    @GetMapping("/{id}")
    public R findByUserName(@PathVariable Integer id){
        Device getDeviceById = deviceService.getOne(new QueryWrapper<Device>().eq("id", id));
        return R.success(getDeviceById);
    }



    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        deviceService.deleteById(id);
        return R.success();
    }

    @PostMapping("/bath/del")
    public R deleteBath(@RequestBody List<Integer> ids){
        deviceService.deleteBatchByIds(ids);
        return R.success();
    }

    @GetMapping("/page")
    public R findPage(@RequestParam Integer pageNum,
                      @RequestParam Integer pageSize,
                      @RequestParam String name){
        // 构造分页信息和查询参数
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("startIndex", (pageNum - 1) * pageSize);
        params.put("pageSize", pageSize);

        // 执行查询操作并返回结果
        List<DeviceDto> deviceList = deviceMapper.selectDevicePage(params);
        return R.success(deviceList);
    }
    @GetMapping("/total")
    public R getTotal(){
        QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>();
        deviceQueryWrapper.eq("del_flag",0);
        return R.success(deviceMapper.selectCount(deviceQueryWrapper));
    }

    //类型下拉列表
    @GetMapping("/type/list")
    public R findTypes(){
        return R.success(typeService.list());
    }

    //人员下拉列表
    @GetMapping("/user/list")
    public R findUsers(){
        return R.success(userService.list());
    }



    @GetMapping("/count/list")
    public R deviceCountLiat(){
        List<DeviceDto> deviceStatusCount = deviceStatusCountService.getDeviceStatusCount();

        return R.success(deviceStatusCount);
    }

}
